package com.ruoyi.project.common.appnotice.controller;

import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.common.appchannel.domain.AppChannel;
import com.ruoyi.project.common.appchannel.service.IAppChannelService;
import com.ruoyi.project.common.appnotice.domain.AppNotice;
import com.ruoyi.project.common.appnotice.domain.GreyIdx;
import com.ruoyi.project.common.appnotice.service.IAppNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统弹窗Controller
 *
 * @author shixiangwen
 * @date 2022-03-18
 */
@Controller
@RequestMapping("/common/appnotice")
public class AppNoticeController extends BaseController {

    private String prefix = "common/appnotice";

    @Autowired
    private IAppNoticeService appNoticeService;

    @Autowired
    private IAppChannelService appChannelService;

    @RequiresPermissions("common:appnotice:view")
    @GetMapping()
    public String appnotice() {
        return prefix + "/appnotice";
    }

    /**
     * 查询系统弹窗列表
     */
    @RequiresPermissions("common:appnotice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppNotice appNotice) {
        startPage();
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        return getDataTable(list);
    }

    /**
     * 导出系统弹窗列表
     */
    @RequiresPermissions("common:appnotice:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppNotice appNotice) {
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        ExcelUtil<AppNotice> util = new ExcelUtil<AppNotice>(AppNotice.class);
        return util.exportExcel(list, "appnotice");
    }

    /**
     * 新增系统弹窗
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        List<GreyIdx> greyIdxs = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            GreyIdx greyIdx = new GreyIdx();
            greyIdx.setIdx(i + 1);
            greyIdxs.add(greyIdx);
        }
        mmap.addAttribute("greyIdxs", greyIdxs);

        mmap.addAttribute("channels", appChannelService.selectAppChannelList(new AppChannel()));

        return prefix + "/add";
    }

    /**
     * 新增保存系统弹窗
     */
    @RequiresPermissions("common:appnotice:add")
    @Log(title = "系统弹窗", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppNotice appNotice) {
        return toAjax(appNoticeService.insertAppNotice(appNotice));
    }

    /**
     * 修改系统弹窗
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AppNotice appNotice = appNoticeService.selectAppNoticeById(id);

        List<Integer> idxs = new ArrayList<>();
        if (!StringUtils.isEmpty(appNotice.getGreyIdx())) {
            idxs = JsonUtil.toListObject(appNotice.getGreyIdx(), Integer.class);
        }
        List<GreyIdx> greyIdxs = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int idx = i + 1;
            GreyIdx greyIdx = new GreyIdx();
            greyIdx.setIdx(idx);
            if (idxs.contains(idx)) {
                greyIdx.setFlag(true);
            } else {
                greyIdx.setFlag(false);
            }
            greyIdxs.add(greyIdx);
        }

        List<AppChannel> channels = appChannelService.selectAppChannelList(new AppChannel());
        if (!StringUtils.isEmpty(appNotice.getChannel())) {
            List<String> noticeChannels = JsonUtil.toListObject(appNotice.getChannel(), String.class);
            for (AppChannel model : channels) {
                if (noticeChannels.contains(model.getChannelId())) {
                    model.setFlag(true);
                }
            }
        }

        mmap.addAttribute("channels", channels);

        mmap.put("appNotice", appNotice);
        mmap.put("greyIdxs", greyIdxs);

        return prefix + "/edit";
    }

    /**
     * 修改保存系统弹窗
     */
    @RequiresPermissions("common:appnotice:edit")
    @Log(title = "系统弹窗", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppNotice appNotice) {
        return toAjax(appNoticeService.updateAppNotice(appNotice));
    }

    /**
     * 删除系统弹窗
     */
    @RequiresPermissions("common:appnotice:remove")
    @Log(title = "系统弹窗", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(appNoticeService.deleteAppNoticeByIds(ids));
    }
}
